/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrator
 */
public class ExportBill {
    private ArrayList<entity.Room> roomList;
    private entity.Customer customer;
    private entity.Payment pay;
    private int bill;
    public ExportBill(int id, int bill){
       pay = new entity.Payment();
       pay =PaymentController.getByIdCustomer(id);
       this.bill=bill;
       roomList =new ArrayList<>();
       roomList =BookingController.getRoomByCustomer(id);
       customer = new entity.Customer();
       customer = pay.getCustomer();       
    }  
      
    public void exportTable(JTable table, File file) throws IOException {  
        
     TableModel model = table.getModel();  
     FileWriter out = new FileWriter(file);  
     for(int i=0; i < model.getColumnCount();i++) {  
         out.write(model.getColumnName(i)+"\t");  
        }  
    out.write("\n");  
    for(int i=0; i < model.getRowCount();i++){  
     for(int j=0;j < model.getColumnCount();j++){    
//        if(j==0) {   
            out.write(model.getValueAt(i,j).toString() + "\t"); 
//        } else {   
//            out.write("\""+model.getValueAt(i, j).toString()+"\t"+"\"");  
//        }  
     }  
     out.write("\n");  
    }  
    out.close();  
    System.out.println("write to " + file);  
   }   
    public void printBill(String fileName){
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat sd = new SimpleDateFormat("ddMMyyyy");
        Writer wt = null;
        try {
            File file = new File(fileName);            
            wt = new BufferedWriter(new OutputStreamWriter(
                 new FileOutputStream(fileName), "UTF8"));
            
            wt.append("<!DOCTYPE HTML>");
            wt.append("<head>");
            wt.append("<meta http-equiv=\"content-type\" content=\"text/html\" />");
            wt.append("<meta name=\"author\" content=\"Đức Trung\" />");
            wt.append("<title>Hóa đơn</title>   ");
            wt.append(css);
            wt.append("</head>");
            wt.append("<body>");
            wt.append("<div id=\"bill\">");
            wt.append("<h1> HÓA ĐƠN THANH TOÁN</h1>");
            wt.append("<h5> Mã hóa đơn: "+sd.format(new Date())+bill+"</h5>");
            wt.append("<div id=\"day\">Ngày lập: "+sdf.format(new Date())+"</div>");
            wt.append("<div id=\"customer\"> ");
            wt.append("<table id=\"a\">");
            wt.append("<tr><td style=\"width: 150px;\">Khách sạn: </td><td> Hướng Dương</td></tr>");
            wt.append("<tr><td style=\"width: 150px;\">Liên hệ: </td><td>  Xã Đàn , Hà Nội</td></tr>");
            wt.append("<tr><td style=\"width: 150px;\">Tên khách hàng: </td><td>"+this.customer.getName()+"</td></tr>");
            wt.append("<tr><td>Địa chỉ :</td><td>"+this.customer.getAddress()+"</td></tr>");
            wt.append("<tr><td>Điện thoại :</td><td>"+this.customer.getPhone()+"</td></tr>");
            wt.append(" <tr><td>Hình thức thanh toán: </td><td>Tiền mặt</td></tr>");
            wt.append(" </table>");
            wt.append("<table id=\"b\">");
            wt.append(" <tr>");
            wt.append("<th>Tên phòng</th>");
            wt.append(" <th>Giá </th>");
            wt.append("<th>Loại phòng </th>");
            wt.append("<th>Ngày trả phòng</th>");
            wt.append("</tr>");
            for(int i=0;i<roomList.size();i++){
            wt.append("<tr class=\"room\"><td>Phòng "+roomList.get(i).getName()
                    +"</td><td>"+roomList.get(i).getRoomtype().getCost()
                    +"VNĐ</td><td>"+roomList.get(i).getRoomtype().getName() 
                    +"</td><td>"+customer.getInfo().getCheckout()+"</td></tr>");
            }
            wt.append(" <th></th><th></th><th></th><th></th>");
            wt.append("<tr><td>Tiền đặt cọc</td><td>"+customer.getInfo().getPayment()
                    +"</td><td>Tổng trả</td><td>"+customer.getInfo().getTotal()
                    +"$</td></tr>");
            wt.append("</table>");
            wt.append("<div class=\"kh\">Khách hàng (ký tên)</div> <div class=\"kh\">Kế toán (ký tên)</div>");
            wt.append("</div>");
            wt.append("</body>");
            wt.append("</html>");
            
            wt.flush();
            wt.close();
            
        } catch (IOException ex) {
            Logger.getLogger(ExportBill.class.getName()).log(Level.SEVERE, null, ex);
        }  finally {
            try {
                wt.close();
            } catch (IOException ex) {
                Logger.getLogger(ExportBill.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        openFile(fileName);
        
    }
    private void openFile(String path){    
      try {
                    Desktop desktop = null;
                    if (Desktop.isDesktopSupported()) {
                        desktop = Desktop.getDesktop();
                    }
                        desktop.open(new File(path));
                    } catch (IOException ioe) {
                        System.out.println(ioe.toString());
      }
    }
    private String css ="<style type=\"text/css\">\n" + "body {\n" + "font: 13px Arial;\n" + 
            "margin: 50px 200px 200px 50px; \n" + "} \n" + "#bill {\n" + "padding: 4px; margin: 8px; float: left;\n"
            +"\n" + "-moz-border-radius: 4px; -webkit-border-radius: 4px;  \n"
            +"}" + "#hotel {" +"color : red;"+ "" + "padding: 7px;" + "height:150px;" + "width: 500px; "
            + "}#hotel td{height: 40px;}" + "#customer {color : red;padding: 7px;height:550px;width: 500px;}"
            + "#customer td{height: 40px;}#bill h1 {text-align: center;} #bill h5 {text-align: right;}" + "#day{font: italic;text-align: right;}" +
            "#bill #customer table#b{border: 1px solid black;text-align: center;}" + 
            "#bill #customer table#b td{width: 120px; height: 30px;} #bill #customer table#b th{border-bottom: 1px solid #aaa;} " +
            ".kh{  margin: 50px 0 0 0; width: 250px;  float: left  } #sign{margin: 50px 0 0 0; }</style>  ";
            
}
